# Contributor: Bart Ribbers <bribbers@disroot.org>
# Maintainer: Bart Ribbers <bribbers@disroot.org>
pkgname=dolphin-emu
pkgver=2409
pkgrel=0
_commit_cubeb="54217bca3f3e0cd53c073690a23dd25d83557909"
_commit_tinygltf="c5641f2c22d117da7971504591a8f6a41ece488b"
_commit_gtest="58d77fa8070e8cec2dc1ed015d66b454c8d78850"
_commit_implot="cc5e1daa5c7f2335a9460ae79c829011dc5cef2d"
_commit_mgba="8739b22fbc90fdf0b4f6612ef9c0520f0ba44a51"
_commit_minizip_ng="3eed562ef0ea3516db30d1c8ecb0e1b486d8cb70"
_commit_rcheevos="d54cf8f1059cebc90a6f5ecdf03df69259f22054"
_commit_spirv="50b4d5389b6a06f86fb63a2848e1a7da6d9755ca"
_commit_vulkan_memory="009ecd192c1289c7529bff248a16cfe896254816"
_commit_zlib="ce01b1e41da298334f8214389cc9369540a7560f"
 # Upstream doesn't actively support non 64-bit platforms and they're too slow
 # to emulate any games anyway
arch="x86_64 aarch64 riscv64"
url="https://dolphin-emu.org"
pkgdesc="Gamecube / Wii emulator"
license="GPL-2.0-or-later"
makedepends="
	bluez-dev
	cmake
	curl-dev
	enet-dev
	eudev-dev
	ffmpeg-dev
	fmt-dev
	gtest-dev
	hidapi-dev
	libevdev-dev
	libpng-dev
	libspng-dev
	libusb-dev
	libx11-dev
	libxi-dev
	libxrandr-dev
	lz4-dev
	lzo-dev
	mesa-dev
	mbedtls2-dev
	miniupnpc-dev
	ninja
	pugixml-dev
	pulseaudio-dev
	qt6-qtbase-dev
	qt6-qtsvg-dev
	sdl2-dev
	sfml-dev
	vulkan-headers
	vulkan-loader-dev
	xz-dev
	xxhash-dev
	zlib-dev
	zstd-dev
	"
# The following dependencies are required, but atm not supported for using system wide libraries
# minizip-dev soundtouch-dev gtest-dev
source="$pkgname-$pkgver.tar.gz::https://github.com/dolphin-emu/dolphin/archive/refs/tags/$pkgver.tar.gz
	https://github.com/mozilla/cubeb/archive/$_commit_cubeb/cubeb-$_commit_cubeb.tar.gz
	https://github.com/syoyo/tinygltf/archive/$_commit_tinygltf/tinygltf-$_commit_tinygltf.tar.gz
	https://github.com/google/googletest/archive/$_commit_gtest/googletest-$_commit_gtest.tar.gz
	https://github.com/epezent/implot/archive/$_commit_implot/implot-$_commit_implot.tar.gz
	https://github.com/mgba-emu/mgba/archive/$_commit_mgba/mgba-$_commit_mgba.tar.gz
	https://github.com/zlib-ng/minizip-ng/archive/$_commit_minizip_ng/minizip-ng-$_commit_minizip_ng.tar.gz
	https://github.com/RetroAchievements/rcheevos/archive/$_commit_rcheevos/rcheevos-$_commit_rcheevos.tar.gz
	https://github.com/KhronosGroup/SPIRV-Cross/archive/$_commit_spirv/spirv-cross-$_commit_spirv.tar.gz
	https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator/archive/$_commit_vulkan_memory/VulkanMemoryAllocator-$_commit_vulkan_memory.tar.gz
	https://github.com/zlib-ng/zlib-ng/archive/$_commit_zlib/zlib-ng-$_commit_zlib.tar.gz
	gcc13.patch
	"
subpackages="$pkgname-doc $pkgname-lang"
builddir="$srcdir/dolphin-$pkgver"

prepare() {
	default_prepare

	git init -q

	rmdir Externals/mGBA/mgba
	mv "$srcdir"/mgba-$_commit_mgba Externals/mGBA/mgba

	rmdir Externals/spirv_cross/SPIRV-Cross
	mv "$srcdir"/SPIRV-Cross-$_commit_spirv Externals/spirv_cross/SPIRV-Cross

	rmdir Externals/tinygltf/tinygltf
	mv "$srcdir"/tinygltf-$_commit_tinygltf Externals/tinygltf/tinygltf

	rmdir Externals/zlib-ng/zlib-ng
	mv "$srcdir"/zlib-ng-$_commit_zlib Externals/zlib-ng/zlib-ng

	rmdir Externals/cubeb/cubeb
	mv "$srcdir"/cubeb-$_commit_cubeb Externals/cubeb/cubeb

	rmdir Externals/gtest
	mv "$srcdir"/googletest-$_commit_gtest Externals/gtest

	rmdir Externals/rcheevos/rcheevos
	mv "$srcdir"/rcheevos-$_commit_rcheevos Externals/rcheevos/rcheevos

	rmdir Externals/implot/implot
	mv "$srcdir"/implot-$_commit_implot Externals/implot/implot

	rmdir Externals/VulkanMemoryAllocator
	mv "$srcdir"/VulkanMemoryAllocator-$_commit_vulkan_memory Externals/VulkanMemoryAllocator

	rmdir Externals/minizip-ng/minizip-ng
	mv "$srcdir"/minizip-ng-$_commit_minizip_ng Externals/minizip-ng/minizip-ng

	# Make sure we never use non-system libraries except the ones that are not
	# supported being used system-wide by removing them from the Externals
	# folder
	KEEP_SOURCES="
		Bochs_disasm
		FatFs
		FreeSurround
		VulkanMemoryAllocator
		cpp-optparse
		cubeb
		expr
		glslang
		gtest
		imgui
		implot
		mGBA
		minizip-ng
		picojson
		rangeset
		rcheevos
		soundtouch
		spirv_cross
		tinygltf
		zlib-ng
		"

	# Move the libraries we want to keep out of the externals folder
	for s in $KEEP_SOURCES; do
		mv -v "Externals/$s" .
	done

	# Remove the rest
	rm -r Externals/*

	# Move them back
	for s in $KEEP_SOURCES; do
		mv -v "$s" "Externals/"
	done
}

build() {
	# Enable a generic build for riscv64
	# This enables building for the architecture but disables JIT and is thus slow
	# TODO: see if support for riscv64 has been enabled upstream and disable the generic build
	case "$CARCH" in
		riscv64) _generic=ON ;;
		*) _generic=OFF ;;
	esac

	# analytics is opt-in, a pop up box at first launch, thus fine
	CFLAGS="$CFLAGS -O2" \
	CXXFLAGS="$CXXFLAGS -O2" \
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DENABLE_LLVM=OFF \
		-DENABLE_GENERIC=$_generic \
		-DDISTRIBUTOR="alpinelinux.org" \
		-DUSE_DISCORD_PRESENCE=OFF \
		-DENABLE_AUTOUPDATE=OFF \
		-DENABLE_ANALYTICS=ON \
		-DUSE_SANITIZERS=OFF \
		-DWITH_SANITIZER=OFF \
		-DUSE_SYSTEM_FMT=ON
	cmake --build build
}

check() {
	cd build
	CTEST_OUTPUT_ON_FAILURE=TRUE ctest
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	install -Dm 644 Data/51-usb-device.rules -t "$pkgdir"/usr/lib/udev/rules.d/
}

sha512sums="
4bbcf66cda4b87ecabc791c8fe18c2732d4f1dc8b7744e34e3c267e3e05468dc7c66abd29c4cfd9e165ee0cb46f3b79ef7120069ac1e709ea8858465c2b20df7  dolphin-emu-2409.tar.gz
7ba1cd5b74d49512d2ebf9b07889ee3ad46559afe3293bcbebae0613661f7502f0baaa20b6723738c21b2d71f9d8f72ed1252703533507dec5d2b5ad84784017  cubeb-54217bca3f3e0cd53c073690a23dd25d83557909.tar.gz
486bcf32be137dea5746117074dc51bc978290698de7831e1c4bbaa4acb56ed99fc34c38e69878a955b3e5f10f43eab07b0637164f822beae0fddd099aa0c56d  tinygltf-c5641f2c22d117da7971504591a8f6a41ece488b.tar.gz
d8153c426e4f9c89a74721cc4a24dfcaf319810f4f10aa25fc972f99da2d96d66bc840cf2f51b756fef6b1ca47e8d2c8633f5862cc24e34d57309ad48802124a  googletest-58d77fa8070e8cec2dc1ed015d66b454c8d78850.tar.gz
ab9935e759f38d19e6f60ed195c22018929ba46fccf783f4486346d7ffb7040b6097bc4c3d3b3577cc91f0c413684e07449a24bebf3db9c937638092c181de92  implot-cc5e1daa5c7f2335a9460ae79c829011dc5cef2d.tar.gz
782d456b7bf827591c774f146a88821b41b3b936faefddb5aee82bb0dd16ca7ad7228a1c331e1e517eac40a71cc4c94cc44bee12560946328c8433e1c6327225  mgba-8739b22fbc90fdf0b4f6612ef9c0520f0ba44a51.tar.gz
f76a2cefd7d9ad47f8c3c73b3483b657fab0507605e0b29895eaec759718a65f0d2915ea7022831f3f98493891a1ab4f5035971bf09d4860abbf4cde0b091869  minizip-ng-3eed562ef0ea3516db30d1c8ecb0e1b486d8cb70.tar.gz
ba9281099d9ef335d881cdad8fb43d89461eb7b77dbd6c6c61699211e64a41681e38974c7c40e9cb42773a5fdc69f124d1e3c9093d10d3ddeb31529234609a30  rcheevos-d54cf8f1059cebc90a6f5ecdf03df69259f22054.tar.gz
237bf9c902c23721592bff44b493198bc6b732cbaf4712b1bf56bfe1df76e7290fadec7cccd1694fccd808a1f57b681f696c323ef616f547bb54ddc97c604cea  spirv-cross-50b4d5389b6a06f86fb63a2848e1a7da6d9755ca.tar.gz
3a47a4bc81562b96f598f357b803d2219fa167e6eb87779837efa6e85fc6eaff8e1cfbdea0935117a7b3c630cc99944b94e91823eafa8a94d80b2c459f33c317  VulkanMemoryAllocator-009ecd192c1289c7529bff248a16cfe896254816.tar.gz
73f4e56410ae298ef211ebab52b1a0d3d5bfae91c2de3cd294b56cd0a78ab50e1203b396c7df462f360c1aaaa9392ec5625473d1e9eb084cc64bc813e28d12a1  zlib-ng-ce01b1e41da298334f8214389cc9369540a7560f.tar.gz
8cdd88c93c49325de4cae62af14a14576f679c5b4c1350fb835e7007916d228811ecf5ba9e526760bd2388c69c46990fa33d2dd8bbb1b673dc7e6896ea958b44  gcc13.patch
"
